<template>
  <div class="state-filter">
    <div class="state-filter__items">
      <div class="state-item">
        <div class="title">状态筛选</div>
        <el-select
          v-model="filterStatusValue.value"
          :teleported="false"
          multiple
          collapse-tags
          collapse-tags-tooltip
          :max-collapse-tags="2"
          placeholder="请选择（可多选）"
          clearable
        >
          <el-option v-for="item in filterStatus" :key="item.value" :label="item.label" :value="item.value" />
        </el-select>
      </div>
      <div class="day-item">
        <div class="title">天数筛选</div>
        <div class="day-item__option">
          <el-select style="width: 88px" :teleported="false" v-model="dayFilterData.filterId" @change="changeDateType">
            <el-option v-for="item in dayData.dayFilter" :key="item.filterId" :label="item.title" :value="item.filterId" />
          </el-select>
          <el-select style="width: 72px; margin: 0 4px" :teleported="false" v-model="dayFilterData.compare">
            <el-option v-for="item in dayData.dayFilterCondition" :key="item.value" :label="item.label" :value="item.value" />
          </el-select>
          <el-input style="width: 92px" type="number" :min="1" v-model="dayFilterData.value[0]" @input="changeInput" />
        </div>
      </div>
    </div>
    <div class="state-filter__buttons">
      <el-button type="primary" class="buttons-cancel" link size="default" @click="handelReset">重置</el-button>
      <el-button type="primary" class="buttons-confirm" size="default" @click="handelConfirm">确定</el-button>
    </div>
  </div>
</template>

<script setup>
import { ref, getCurrentInstance, watch } from "vue";
import { cloneDeep } from "lodash";
import * as dayData from "./config";
import { filterStatus } from "@/views/engineeringManage/config/baseConfig";

const props = defineProps({
  filterList: {
    type: Array,
    default: () => [
      [
        {
          filterId: "build_status",
          title: "状态筛选",
          condition: [
            {
              label: "等于",
              value: "in"
            },
            {
              label: "不等于",
              value: "notIn"
            }
          ],
          option: {
            type: "MultipleSelect",
            lazy: false,
            dataSource: {},
            data: [
              {
                label: "未开始",
                value: "not_start"
              },
              {
                label: "正常",
                value: "ing"
              },
              {
                label: "超期",
                value: "time_out"
              },
              {
                label: "延期风险",
                value: "delay_risk"
              },
              {
                label: "已停工",
                value: "shutdown"
              },
              {
                label: "已交付",
                value: "complete"
              }
            ]
          },
          compare: "in",
          value: []
        },
        {
          filterId: "time_out_day",
          title: "超期天数",
          condition: [
            {
              label: "大于",
              value: "gt"
            },
            {
              label: "小于",
              value: "lt"
            },
            {
              label: "等于",
              value: "eq"
            }
          ],
          option: {
            type: "InputSelect",
            lazy: false,
            dataSource: {},
            data: []
          },
          compare: "gt",
          value: []
        },
        {
          filterId: "delay_risk_day",
          title: "延期天数",
          condition: [
            {
              label: "大于",
              value: "gt"
            },
            {
              label: "小于",
              value: "lt"
            },
            {
              label: "等于",
              value: "eq"
            }
          ],
          option: {
            type: "InputSelect",
            lazy: false,
            dataSource: {},
            data: []
          },
          compare: "gt",
          value: []
        },
        {
          filterId: "shutdown_days",
          title: "停工天数",
          condition: [
            {
              label: "大于",
              value: "gt"
            },
            {
              label: "小于",
              value: "lt"
            },
            {
              label: "等于",
              value: "eq"
            }
          ],
          option: {
            type: "InputSelect",
            lazy: false,
            dataSource: {},
            data: []
          },
          compare: "gt",
          value: []
        }
      ]
    ]
  }
});
const emit = defineEmits(["handelFilterData"]);

const { proxy } = getCurrentInstance();

// 状态
const filterStatusValue = ref({});
// 天数筛选
const dayFilterData = ref({
  id: "time_out_day", // 默认超期
  compare: "gt",
  value: [""]
});

/**
 * 校验天数
 */
const numberInputReg = /^[1-9]\d*$/;
const changeInput = e => {
  if (!numberInputReg.test(e)) {
    dayFilterData.value.value = [""];
    proxy.$modal.msgError("请输入正整数");
  }
};
/**
 * 切换天数筛选类型
 */
const changeDateType = e => {
  dayData.dayFilter.some(item => {
    if (item.filterId === e) {
      dayFilterData.value = {
        ...item
      };
      return true;
    }
  });
};
watch(
  () => props.filterList,
  newVal => {
    filterStatusValue.value = cloneDeep(newVal[0]);
    dayFilterData.value = cloneDeep(newVal[1]);
  },
  { deep: true, immediate: true }
);

const handelReset = () => {
  filterStatusValue.value.value = [];
  dayFilterData.value.filterId = "time_out_day";
  dayFilterData.value.compare = "gt";
  dayFilterData.value.value = [""];
  emit("handelFilterData", [cloneDeep(filterStatusValue.value), cloneDeep(dayFilterData.value)]);
};
const handelConfirm = () => {
  emit("handelFilterData", [cloneDeep(filterStatusValue.value), cloneDeep(dayFilterData.value)]);
};
</script>

<style scoped lang="scss">
@import "stateFilter";
</style>
